package br.inf.ufrgs.usixml4desktop.rendering.javadesktop.components;

import java.lang.reflect.Method;

import javax.swing.JButton;

import org.jdom.Element;

import br.inf.ufrgs.usixml4desktop.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4desktop.rendering.UsiXMLElementRenderer;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.UsiXMLJavaDesktopInterfaceRenderer;
import br.inf.ufrgs.usixml4desktop.utils.PrimitiveElementParser;

public class UsiXMLJavaDesktopButtonRenderer extends UsiXMLElementRenderer {

	public UsiXMLJavaDesktopButtonRenderer(UsiXMLJavaDesktopInterfaceRenderer usiXMLInterfaceRenderer) {
		super(usiXMLInterfaceRenderer);
	}

	@Override
	public UsiXMLElementRenderer getNextRenderer() {
		return new UsiXMLJavaDesktopInputTextRenderer(
				(UsiXMLJavaDesktopInterfaceRenderer)this.usiXMLInterfaceRenderer);
	}

	@Override
	public String getValidElementName() {
		return "button";
	}

	@Override
	public Object processRenderization(Element element) throws ParsingErrorException {
		
		String name = element.getAttributeValue("name");
		String isEnabled = element.getAttributeValue("isEnabled");
		String isVisible = element.getAttributeValue("isVisible");
		
		PrimitiveElementParser parser = new PrimitiveElementParser();
		
		JButton button = new JButton();
		button.setName(name);
		button.setEnabled(parser.parseBoolean(isEnabled));
		button.setVisible(parser.parseBoolean(isVisible));
		
		try {
			Method contentMethod = JButton.class.getMethod("setText",new Class[]{String.class});
			this.insertContent(element,button,contentMethod);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
				
		return button;
	}

	@Override
	public boolean validateElement(Element element) {
		return true;
	}

}
